<?php

chdir(dirname(__FILE__)."/../..");
$path = getcwd();
chdir(dirname(__FILE__));

include_once $path."/../lib/core.class.php";
include 'shiptrack.inc';


class myShipTrack extends IPlugin 
{
    var $pagespool;

    function myShipTrack($parent = null)
    {
        IPlugin::IPlugin($parent);
        $this->tplfolder = dirname(__FILE__)."/tpl";
        $this->pagespool = null;
        $this->initialize();
    }
    
    function getPluginTag() { return "shiptrack"; }
    function getPluginName() { return "Shipling tracking fplugin"; }
    function getPluginVersion() { return "1.0.0"; }

    function getProperty($p = null)
    {
        if(null == $p)
        {
            return array("MenuTitle", "MenuBlockTitle");
        }
        $result = "";
        switch ($p)
        {
            case "MenuBlockTitle": { return "Shipling tracking fplugin"; } break;
            case "MenuTitle" : { return "shiptrack"; } break;
        };
        return $result;
    }
    
    function initialize()
    {
        $this->pagespool = array();
        if ($handle = opendir($this->tplfolder))
        {
            while (false !== ($file = readdir($handle)))
            {
                if("."==$file || ".."==$file || !file_exists($this->tplfolder."/".$file))
                {
                    continue;
                }
                $fl = split("\.", $file);
                $this->pagespool[] = $fl[0];
            }
            closedir($handle);
        }
        $this->pagespool = array_flip($this->pagespool);
        $core = &getCoreInstance();
        $core->setValues("staticpage_url", "?".$this->getURL()."&page=");
//      debug($this->pagespool);
    }
    
    // ----------------------------------------------------------------------------------
    // --- [ Handling all actions ] -----------------------------------------------------
    // ----------------------------------------------------------------------------------

    function doAction($params)
    {
        if(null != ($result = IPlugin::doAction($params)))
        {
            return $result;
        }

    
        switch ($_POST["event"]) {
        case "none": {

        echo 'event=none';
              } break;   
  


    
        default:
                break;

        }
    }


    // ----------------------------------------------------------------------------------
    // --- [ Render pages ] -------------------------------------------------------------
    // ----------------------------------------------------------------------------------

    function Render($params)
    {
        global $_G;
        if(null != ($result = IPlugin::Render($params)))
        {
            return $result;
        }
        $core = &getCoreInstance();
        $render = &$core->framework->smarty;
        $db = &$core->getProperty("db");

        switch ($_G["page"]) {

    
            case "check" :
                $this->renderResult($core, $render, $db);
                $page = "result.tpl.php";
                break;

            default:
                $this->renderIndexPage($core, $render, $db);
                $page = "enterdata.tpl.php";
                break;
        }

        
        //debug($page);
        return $render->fetch("file:".$this->tplfolder."/".$page);
    }
    
// ----------------------------------------------------------------------------------
// --- [ Render Index pages ] -------------------------------------------------------------
// ----------------------------------------------------------------------------------
    
    function renderIndexPage(&$core, &$render, &$db)
    {
$transport = array (

1 => array('key'   =>'UPS',
           'value' => 'UPS'),

2 => array('key'   =>'FEDEX',
           'value' => 'FEDEX')
/*
3 => array('key'   =>'USPS',
           'value' => 'US Post Office')
*/

/*
'ROADWAY'     => 'Roadway',
'AX'          => 'BAX Global',
'NEWPENN'     => 'NEW PENN',
'ABF'         => 'ABF',
'REDSTAR'     => 'Red Star',
'YELLOW'      => 'Yellow',
'DHL'         => 'DHL',
'FFE'         => 'Fedex Freight East',
'EMERY'       => 'Emery',
'GOD'         => 'G.O.D.',
'OLDDOMINION' => 'Old Dominion',
'USPS'        => 'US Post Office',
'CCX'         => 'CCX' 
*/
                   );

$render->assign("transport", $transport);       
$url = "?".$this->getURL()."&page=check";
$render->assign("aurl", $url);       


        if (isset($_GET["cid"]))
        $id_catalog = $_GET["cid"];
        else
        $id_catalog = 0;

//        $render->assign("BBLA", 'BBLAAAAAA');       
    }
    
// ----------------------------------------------------------------------------------
// --- [ Render Index pages ] -------------------------------------------------------------
// ----------------------------------------------------------------------------------

    function renderResult(&$core, &$render, &$db)
    {
        if (isset($_GET["cid"]))
        $id_catalog = $_GET["cid"];
        else
        $id_catalog = 0;
    $p = $_POST['provider'];
    $n = $_POST['number'];
    $msg = 'here';    
    $MyLink = new ShipTrack();

    if(isset($_GET['n']) and $_GET['n'] != ''){
      $n = str2db($_GET['n']);
    }   
    if(isset($_GET['p']) and $_GET['p'] != ''){
      $p = str2db($_GET['p']);
    }
     
    $a = $MyLink->PrintLink("{$p}","{$n}","{$msg}","","_blank","foobar");
    list($a,$header) = split('!!',$a,2);

    
    if ($p != '' and $n != '' and $header != '')
  {
     header("location: ".$header);
     exit();
  }
//$render->assign("link", $a);       
    else
    $render->assign("error", '1');       

    }



};

?>